package logic

import (
	"context"
	"goods_srv/goodssrv"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGoodsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGoodsLogic {
	return &AddGoodsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddGoodsLogic) AddGoods(req *types.AddGoodsRequest) (resp *types.Response, err error) {
	add, err := l.svcCtx.GoodsSrv.AddGoods(l.ctx, &goodssrv.AddGoodsRequest{
		Title:       req.Title,
		Cover:       req.Cover,
		Price:       float32(req.Price),
		SalesVolume: req.SalesVolume,
		Stock:       req.Stock,
	})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  err.Error(),
			Data: nil,
		}, nil
	}
	return &types.Response{
		Code: 200,
		Msg:  "添加商品成功",
		Data: map[string]interface{}{
			"GoodsId": add.GoodsId,
		},
	}, nil
}
